package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.result.PageResult;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public PageResult orderPageQuery(OrdersPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<OrderVO> page = ordersMapper.orderPageQuery(dto);
        long total = page.getTotal();
        List<OrderVO> result = page.getResult();
        return new PageResult(total, result);

    }

    @Override
    public Orders getById(Long id) {
        Orders orders = ordersMapper.getById(id);
        return orders;
    }

    @Override
    public void cancelOrder(OrdersCancelDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setCancelTime(LocalDateTime.now());
        orders.setStatus(6);
        ordersMapper.cancelOrder(orders);
    }

    @Override
    public OrderStatisticsVO statisticsOrder() {
        Integer toBeConfirmed = ordersMapper.toBeConfirmed();
        Integer confirmed= ordersMapper.confirmed();
        Integer deliveryInProgress = ordersMapper.deliveryInProgress();
       OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
       orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
       orderStatisticsVO.setConfirmed(confirmed);
       orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
       return orderStatisticsVO;
    }

    @Override
    public void rejection(OrdersRejectionDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setStatus(7);
        ordersMapper.rejection(orders);
    }

    @Override
    public void complete(Long id) {
        Orders build = Orders.builder().id(id).status(5).deliveryTime(LocalDateTime.now()).build();
        ordersMapper.complete(build);

    }
}
